//
//  LKAddShopCloudViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/10.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKAddShopCloudViewController.h"

@interface LKAddShopCloudViewController ()
@property(nonatomic,strong)UFFormView *formView;
@property(nonatomic,copy)NSString *id_number;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *volume;

@end

@implementation LKAddShopCloudViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.model) {
        self.navigationItem.title = @"编辑云喇叭";
        [self setNavgationRightBtn];
        [self createEditFormView];
    }else{
        self.navigationItem.title = @"添加云喇叭";
        [self createFormView];
    }

    // Do any additional setup after loading the view.
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"删除" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"id":weakSelf.model.Id}];
        NSString *sign = [NSString encoingWithDic:param];
        [param setObject:sign forKey:@"sign"];
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopCloudDeleteUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            if ([responseObject[@"code"] isEqualToString:@"success"]) {
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadShopCloudListData" object:nil];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            }
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)createEditFormView{
    WS(weakSelf)
    self.formView = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])

            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"云喇叭名称")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.model.name = value;
                })
                .value(weakSelf.model.name);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .placeholder(@"请输入音量百分比值1%~100%")
                .height(RowHeightDefault)
                .title(@"云喇叭音量")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.model.volume = value;
                })
                .value(weakSelf.model.volume);
            }]);
        }])
        .addToSuperView(self.view);
    }];

    [self.formView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_equalTo(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(208);
    }];
    self.formView.scrollEnabled = NO;
    LKBaseButton *bottomButton = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"保存" titleColor:[UIColor whiteColor] layercorne:25 font:[UIFont fontWithName:@"PingFangSC-Medium" size:18] backgroudColor:yellowDefaultColor AndActionBlock:^(UIButton * _Nonnull btn) {
        if ([NSString isBlankString:weakSelf.model.name]) {
            [SVProgressHUD showInfoWithStatus:@"请输入设备名称"];
            return ;
        }
        if ([NSString isBlankString:weakSelf.model.volume]) {
            [SVProgressHUD showInfoWithStatus:@"请输入设备音量"];
            return ;
        }
        NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"id":weakSelf.model.Id,@"id_number":weakSelf.model.id_number,@"model":@"智联博众",@"name":weakSelf.model.name,@"type":@"2",@"volume":weakSelf.model.volume}];
        NSString *sign = [NSString encoingWithDic:param];
        [param setObject:sign forKey:@"sign"];
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopCloudEditUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            if ([responseObject[@"code"] isEqualToString:@"success"]) {
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadShopCloudListData" object:nil];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            }
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
    }];
    [self.view addSubview:bottomButton];
    [bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
        make.height.mas_equalTo(50);
    }];
}

-(void)createFormView{
    WS(weakSelf)
    self.formView = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])

            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"云喇叭型号")
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                })
                .value(@"智联博众");
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"云喇叭名称")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.name = value;
                })
                .value(weakSelf.name);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .placeholder(@"(云喇叭底部4或6位数字)")
                .height(RowHeightDefault)
                .title(@"云喇叭产品ID")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.id_number = value;
                })
                .value(weakSelf.id_number);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .placeholder(@"请输入音量百分比值1%~100%")
                .height(RowHeightDefault)
                .title(@"云喇叭音量")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                    weakSelf.volume = value;
                })
                .value(weakSelf.volume);
            }]);
        }])
        .addToSuperView(self.view);
    }];

    [self.formView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_equalTo(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(208);
    }];
    self.formView.scrollEnabled = NO;
    LKBaseButton *bottomButton = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"提交绑定" titleColor:[UIColor whiteColor] layercorne:25 font:[UIFont fontWithName:@"PingFangSC-Medium" size:18] backgroudColor:yellowDefaultColor AndActionBlock:^(UIButton * _Nonnull btn) {
        if ([NSString isBlankString:weakSelf.id_number]) {
            [SVProgressHUD showInfoWithStatus:@"请输入设备ID"];
            return ;
        }
        if ([NSString isBlankString:weakSelf.name]) {
            [SVProgressHUD showInfoWithStatus:@"请输入设备名称"];
            return ;
        }
        if ([NSString isBlankString:weakSelf.volume]) {
            [SVProgressHUD showInfoWithStatus:@"请输入设备音量"];
            return ;
        }
        NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"id_number":weakSelf.id_number,@"model":@"智联博众",@"name":weakSelf.name,@"type":@"2",@"volume":weakSelf.volume}];
        NSString *sign = [NSString encoingWithDic:param];
        [param setObject:sign forKey:@"sign"];
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopCloudAddUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            if ([responseObject[@"code"] isEqualToString:@"success"]) {
                [SVProgressHUD showInfoWithStatus:@"绑定成功"];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadShopCloudListData" object:nil];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            }
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
    }];
    [self.view addSubview:bottomButton];
    [bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
        make.height.mas_equalTo(50);
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
